class_name MeleeBehavior
extends WeaponBehavior



var _weapon_punch: WeaponPunch


func _ready():
	super._ready()
	_weapon_punch = owner



func execute_attack():
	_weapon_punch.set_attacking(true)
	_attack_animation()



func _attack_animation():
	var tween := create_tween()
	tween.finished.connect(_on_attack_finished)

	var recoil_pos := Vector2(_weapon_punch._start_position.x - _weapon_punch.data.weapon_stats.recoil, _weapon_punch._start_position.y)
	tween.tween_property(_weapon_punch.sprite, "position", recoil_pos, _weapon_punch.data.weapon_stats.recoil_duration)

	_weapon_punch.hitbox_component.set_enabled(true)
	_weapon_punch.hitbox_component.setup(get_damage())

	var attack_pos := Vector2(_weapon_punch._start_position.x + _weapon_punch.data.weapon_stats.attack_range, _weapon_punch._start_position.y)
	tween.tween_property(_weapon_punch.sprite, "position", attack_pos, _weapon_punch.data.weapon_stats.attack_duration)

	tween.tween_property(_weapon_punch.sprite, "position", _weapon_punch._start_position, _weapon_punch.data.weapon_stats.knockback_duration)



func _on_attack_finished():
	_weapon_punch.set_attacking(false)
	_weapon_punch.hitbox_component.set_enabled(false)
	_critical = false
